package com.aaa.service;

import com.aaa.dao.UserDao;
import com.aaa.dao.UserRoleDao;
import com.aaa.dao.impl.UserDaoImpl;
import com.aaa.dao.impl.UserRoleDaoImpl;
import com.aaa.entity.Message;
import com.aaa.entity.SysUserEntity;
import com.aaa.entity.User;
import com.aaa.entity.Pager;
import com.aaa.util.PageUtil;

import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService {
    private UserDao dao=new UserDaoImpl();
    @Override
    public Map login(User user) {
        return dao.login(user);
    }

    @Override
    public List queryAll() {
        return dao.queryAll();
    }

    @Override
    public Pager query(int curPage, int pageSize, Map condition) {
        pageSize= PageUtil.PAGE_SIZE;
        return dao.query(curPage, pageSize, condition);
    }


    @Override
    public Map queryid(int user_id) {
        return (Map) dao.queryid(user_id);
    }

    @Override
    public int delete(int user_id) {
        return dao.delete(user_id);
    }

    @Override
    public Message add(SysUserEntity user) {
        Message message;
        //录入用户
        if (user.getUserId()==0)
        {
            int userId = dao.add(user);
            if (userId > 1) {
                //录入用户对应的角色
                UserRoleDao userRoleDao = new UserRoleDaoImpl();
                //遍历角色id数组
                for (int i = 0; i < user.getRoleIds().length; i++) {
                    //用户编号，角色编号
                    userRoleDao.add(Long.valueOf(userId), user.getRoleIds()[i]);
                }
                message = new Message(200, "录入成功");
            } else {
                message = new Message(300, "录入失败");
            }
        }else
        {
            int userId = dao.update(user);
            if (userId > 1) {
                //录入用户对应的角色
                UserRoleDao userRoleDao = new UserRoleDaoImpl();
                //遍历角色id数组
                for (int i = 0; i < user.getRoleIds().length; i++) {
                    //用户编号，角色编号
                    userRoleDao.update(Long.valueOf(userId), user.getRoleIds()[i]);
                }
                message = new Message(200, "编辑成功");
            } else {
                message = new Message(300, "编辑失败");
            }
        }

        return message;
    }

    @Override
    public List selectloginname(String login_name) {
        return dao.selectloginname(login_name);
    }

}

